home *** CD-ROM | disk | FTP | other *** search
/ Screensavers 98 / Screensavers 98.iso / scr / pyro / fclasses.cpp < prev    next >
C/C++ Source or Header  |  2000-03-28  |  23KB  |  706 lines

  1. #include "fclasses.h"
  2. #include "fix.h"
  3.  
  4. int PixelSize = 1;
  5.  
  6. // ############################################################################
  7. // Constructors
  8.  
  9. Flare::Flare (void)
  10. {
  11.     xPos          = yPos = xPlot = yPlot = ground = gravty = thrust = 0;
  12.     gndTerminated = alive = FALSE;
  13.     gravConst     = (1 * Fixed) >> (random (3)+1);// gravity constant... this case it's random.
  14.  
  15.     // trailer buffer settings. 
  16.     overBuf     = FALSE;
  17.     bufPnt      = -1;
  18.     trailLen    = 0;
  19.     trailer     = NULL;
  20.     colour      = RGB(255,255,255);
  21. }
  22.  
  23. Flare::Flare (int  x,           int      y,
  24.               int  thr,         int      grav,
  25.               int  cyc,         int      gnd,
  26.               int  trailLength, COLORREF col)
  27. {
  28.     init (x, y, thr, grav, cyc, gnd, trailLength, col);
  29. }
  30.  
  31. void Flare::init (int  x,           int      y,
  32.                   int  thr,         int     grav,
  33.                   int  cyc,         int      gnd,
  34.                   int  trailLength, COLORREF col)
  35. {
  36.     xPos   = x;
  37.     yPos   = y;
  38.     thrust = thr;
  39.     ground = gnd;              
  40.     cycles = cyc;
  41.     colour = col;
  42.     gravty = grav;
  43.  
  44.     // init internals ***** Change XPos, yPos and assign to xPlot, yPlot *****
  45.     xPlot  = yPlot = 0;
  46.     gndTerminated = FALSE;           // flare hasn't hit the ground!
  47.     alive         = TRUE;
  48.  
  49.     //**** Allocate trailer memory ****
  50.     // If memory is already allocated, but trail length has changed. Then
  51.     // delete/free memory.
  52.     if ((trailer != NULL) && (trailLen != trailLength))
  53.     {
  54.         delete trailer;
  55.         trailer = NULL;
  56.     }
  57.  
  58.     // Allocate memory
  59.     if (trailer == NULL)
  60.        trailer = new SaveXY[trailLength];
  61.  
  62.     // buffer variables for trailer
  63.     trailLen = trailLength;
  64.     overBuf  = FALSE;
  65.     bufPnt   = -1;
  66. }
  67.  
  68. void Flare::removePoint (HDC hdc, int x, int y)
  69. {
  70.     if (y > ground)
  71.        y = ground;
  72.  
  73.     switch (PixelSize)
  74.     {
  75.         case 1:
  76.              SetPixel(hdc, x, y,   0);
  77.              break;
  78.                              
  79.         case 2:
  80.              SetPixel (hdc, x, y, 0);
  81.              SetPixel (hdc, x, y+1, 0);
  82.              break;
  83.  
  84.         case 3:
  85.              SetPixel (hdc, x+1, y-1, 0);
  86.              SetPixel (hdc, x, y, 0);
  87.              SetPixel (hdc, x+1, y, 0);
  88.              break;
  89.                              
  90.         case 4:
  91.              SetPixel(hdc, x, y, 0);
  92.              SetPixel(hdc, x-1, y, 0);
  93.              SetPixel(hdc, x, y-1, 0);
  94.              SetPixel(hdc, x-1, y-1, 0);
  95.     }
  96. }
  97.  
  98. void Flare::showPoint (HDC hdc, int x, int y)
  99. {
  100.     if (y > ground)
  101.        y = ground;
  102.  
  103.     switch (PixelSize)
  104.     {
  105.         case 1:
  106.              SetPixel(hdc, x, y, colour);
  107.              break;
  108.                              
  109.         case 2:
  110.              SetPixel (hdc, x, y,   colour);
  111.              SetPixel (hdc, x, y+1, colour);
  112.              break;
  113.  
  114.         case 3:
  115.              SetPixel (hdc, x+1, y-1, colour);
  116.              SetPixel (hdc, x, y, colour);
  117.              SetPixel (hdc, x+1, y, colour);
  118.              break;
  119.                              
  120.         case 4:
  121.              SetPixel(hdc, x, y,     colour);
  122.              SetPixel(hdc, x-1, y,   colour);
  123.              SetPixel(hdc, x, y-1,   colour);
  124.              SetPixel(hdc, x-1, y-1, colour);
  125.     }
  126. }
  127.  
  128. BOOL Flare::move (HDC hdc)
  129. {
  130.     if (alive)// && (trailer != NULL))
  131.     {
  132.         // update flare
  133.         if (trailLen > 0)          
  134.         {
  135.             // decrement cycles of life for firework
  136.             cycles--;
  137.  
  138.             yPlot  = yPlot + gravty;      // decrease virtical lift
  139.             xPlot  = xPlot + thrust;      // decrease horizontal thrust  
  140.             gravty = gravty - gravConst;  // update gravity pull
  141.  
  142.             // **** Trailer maintenace ****
  143.             // load buffer from start
  144.             if (overBuf == FALSE)
  145.             {
  146.                 // If item exceeds buffer, rollover to start of buffer (queue system)
  147.                 if (bufPnt >= trailLen - 1)
  148.                 {
  149.                     bufPnt    = -1;
  150.                     overBuf   = TRUE;
  151.  
  152.                     removePoint (hdc, trailer [bufPnt+1].x, trailer [bufPnt+1].y);
  153.                 }
  154.  
  155.                 bufPnt++;
  156.                 trailer[bufPnt].x = xPos + (xPlot / Fixed);
  157.                 trailer[bufPnt].y = yPos - (yPlot / Fixed);
  158.             }
  159.             else // cycle thru queue buffer.
  160.             {
  161.                 if (cycles <= trailLen)
  162.                 {
  163.                     removePoint (hdc, trailer [trailLen-1].x, trailer [trailLen-1].y);
  164.                     trailLen--;
  165.                 }
  166.  
  167.                 if (bufPnt >= trailLen - 1)
  168.                    bufPnt = -1;
  169.  
  170.                 bufPnt++;
  171.                 removePoint (hdc, trailer [bufPnt].x, trailer [bufPnt].y);
  172.                 trailer [bufPnt].x = xPos + (xPlot / Fixed);
  173.                 trailer [bufPnt].y = yPos - (yPlot / Fixed);
  174.             }
  175.  
  176.             showPoint (hdc, trailer [bufPnt].x, trailer [bufPnt].y);
  177.         }
  178.         else
  179.         {
  180.             removePoint (hdc, trailer [0].x, trailer [0].y);
  181.             alive = FALSE;
  182.         }
  183.     }
  184.     return alive;
  185. }
  186.  
  187. // ############################################################################
  188. // Destructor
  189. Flare::~Flare(void)
  190. {
  191.     delete trailer;
  192. }
  193.  
  194.  
  195. // ############################################################################
  196. // ############################################################################
  197. // ############################################################################
  198.  
  199. void SkyRocket::init (int  starBNum,
  200.                       int  x,          int      y,
  201.                       int  thr,        int     grav,
  202.                       int  cyc,        int      gnd,
  203.                       int trailLength, COLORREF col)
  204. {
  205.     //**** Allocate explode memory ****
  206.     // If memory is already allocated, but explode number has changed. Then
  207.     // delete/free memory.
  208.     if ((pStarBurst != NULL) && (starBurstNum != starBNum))
  209.     {
  210.         delete pStarBurst;
  211.         pStarBurst = NULL;
  212.     }
  213.  
  214.     if (pStarBurst == NULL)
  215.        pStarBurst = new Flare[starBNum];
  216.  
  217.     // Initialise main rocket ....
  218.     mainRocket.init (x, y, thr, grav, cyc, gnd, trailLength, col);
  219.  
  220.     // Initialise explode variables.
  221.     explode      = FALSE;
  222.     starBurstNum = starBNum;
  223.     colour       = col;
  224.     trailLen     = trailLength;
  225.  
  226.     // Select colour item to change (i.e. increment!)
  227.     // test for single primary colours
  228.     const int incRocAmount = 3;
  229.           int testRGB = GetRValue(colour);
  230.               testRGB += GetGValue(colour);
  231.               testRGB += GetBValue(colour);
  232.  
  233.  
  234.     if (testRGB <= 256)          // Only one colour is active
  235.     {
  236.         // Pick an primary item that isn't set!
  237.         int pick = 0;
  238.         while (pick == 0)
  239.         {
  240.             colItemChange = random (3);
  241.             switch (colItemChange)
  242.             {
  243.                 case 0:
  244.                      if (GetRValue(colour) < 127)
  245.                         pick = -1;
  246.                      break;
  247.  
  248.                 case 1:
  249.                      if (GetGValue(colour) < 127)
  250.                         pick = -1;
  251.                      break;
  252.  
  253.                 case 2:
  254.                      if (GetBValue(colour) < 127)
  255.                         pick = -1;
  256.             }
  257.         } 
  258.     }
  259.     else
  260.         colItemChange = random(3);
  261.  
  262.     // Select the increment style according to current colour value.
  263.     switch (colItemChange)
  264.     {
  265.          // red
  266.          case 0:
  267.               if (GetRValue(colour) < 127)
  268.                  colChangeAmnt = incRocAmount;
  269.               else
  270.                  colChangeAmnt = -incRocAmount;
  271.               break;
  272.  
  273.          // green
  274.          case 1:
  275.               if (GetGValue(colour) < 127)
  276.                  colChangeAmnt = incRocAmount;
  277.               else
  278.                  colChangeAmnt = -incRocAmount;
  279.               break;
  280.  
  281.  
  282.          // blue
  283.          case 2:
  284.              if (GetBValue(colour) < 127)
  285.